package com.xhqgov.manage.quartz;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.xhqgov.manage.controller.BaseController;
import com.xhqgov.manage.vo.AuthHelper;
import org.apache.log4j.Logger;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class AccessTokenQuartz extends BaseController {
    private static final Logger LOGGER = Logger.getLogger(AccessTokenQuartz.class);

    @Resource
    private AuthHelper authHelper;

    /**
     * 获取AccessToken
     * 7200秒内续期
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void getAccessToken(){
        try{
            DefaultDingTalkClient client = new DefaultDingTalkClient(getAccessTokenUrl());
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setCorpid(getCorpId());
            request.setCorpsecret(getCorpSecret());
            request.setHttpMethod(REQUEST_GET);
            OapiGettokenResponse response = client.execute(request);
            authHelper.setAccessToken(client.execute(request).getAccessToken());
            LOGGER.info("---------获取token---------");
        }catch (Exception e){
            LOGGER.error("获取AccessToken失败：",e);
        }

    }
}
